#include "../stdafx.h"
#include "Layer.h"
#include "../Game.h"

CLayer::CLayer(const char* filePath, SGD::Size size, SGD::Point location, float parallax, bool wrap)
{
	SetParallaxIntensity(parallax);
	SetSize(size);
	SetLocation(location);
	m_hBackGround = SGD::GraphicsManager::GetInstance()->LoadTexture(filePath);
	m_bWrap = wrap;
}

CLayer::~CLayer()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hBackGround);
}

void CLayer::Render()
{
	SGD::Point renderPoint(GetLocation().x + Game::GetInstance()->GetCameraPos().x * GetParallaxIntensity(),
		GetLocation().x + Game::GetInstance()->GetCameraPos().x * GetParallaxIntensity());
	if (!GetWrap())
	{
		//Render background
		SGD::GraphicsManager::GetInstance()->DrawTexture(m_hBackGround, renderPoint);
	}
	else
	{
		SGD::Point originPoint = renderPoint;
		while (originPoint.x > 0)
			originPoint.x -= m_szLayerSize.width;
		while (originPoint.y > 0)
			originPoint.y -= m_szLayerSize.height;

		renderPoint.y = originPoint.y;

		while (renderPoint.y < Game::GetInstance()->GetScreenHeight())
		{
			renderPoint.x = originPoint.x;
			while (renderPoint.x < Game::GetInstance()->GetScreenWidth())
			{
				SGD::GraphicsManager::GetInstance()->DrawTexture(m_hBackGround, renderPoint);
				renderPoint.x += m_szLayerSize.width;
			}
			renderPoint.y += m_szLayerSize.height;
		} 
	}
}

void CLayer::Update(float dt)
{

}
